package com.work.yshop.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.work.yshop.R;
import com.work.yshop.model.ProductHistory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 浏览历史适配器
 */
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.HistoryViewHolder> {
    
    private List<ProductHistory> historyList = new ArrayList<>();
    private OnItemClickListener listener;
    
    public void setData(List<ProductHistory> historyList) {
        this.historyList = historyList;
        notifyDataSetChanged();
    }
    
    @NonNull
    @Override
    public HistoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history, parent, false);
        return new HistoryViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull HistoryViewHolder holder, int position) {
        ProductHistory history = historyList.get(position);
        
        // 设置商品信息
        if (history.getProduct() != null) {
            holder.tvName.setText(history.getProduct().getName());
            holder.tvPrice.setText("¥" + history.getProduct().getPrice());
            
            // 加载商品图片
            Glide.with(holder.itemView.getContext())
                    .load(history.getProduct().getImageUrl())
                    .placeholder(R.drawable.placeholder_image)
                    .into(holder.ivProduct);
        }
        
        // 设置浏览时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        String timeStr = sdf.format(new Date(history.getViewTime()));
        holder.tvTime.setText(timeStr);
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(position);
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return historyList.size();
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    
    /**
     * 点击监听器
     */
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    
    /**
     * 历史记录ViewHolder
     */
    static class HistoryViewHolder extends RecyclerView.ViewHolder {
        ImageView ivProduct;
        TextView tvName;
        TextView tvPrice;
        TextView tvTime;
        
        public HistoryViewHolder(@NonNull View itemView) {
            super(itemView);
            ivProduct = itemView.findViewById(R.id.iv_product);
            tvName = itemView.findViewById(R.id.tv_name);
            tvPrice = itemView.findViewById(R.id.tv_price);
            tvTime = itemView.findViewById(R.id.tv_time);
        }
    }
} 